package com.xh3140.xwidget.dialog.view;

import android.content.Context;
import android.content.DialogInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;

import com.xh3140.xwidget.dialog.BaseDialog;
import com.xh3140.xwidget.dialog.listener.OnClickButtonListener;
import com.xh3140.xwidget.dialog.params.FooterParams;

import java.util.Objects;


/**
 * 对话框底部视图
 */
@SuppressWarnings("unused")
public final class FooterView extends LinearLayoutCompat {
  private ButtonView mPositiveButton;
  private ButtonView mNegativeButton;
  private ButtonView mNeutralButton;

  public FooterView(@NonNull Context context) {
    super(context);
    setOrientation(HORIZONTAL);
    setShowDividers(SHOW_DIVIDER_MIDDLE);
    setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  }

  public <D extends BaseDialog<?>> FooterView configView(@NonNull D dialog, @NonNull FooterParams params, @Nullable OnClickButtonListener<D> listener) {
    removeAllViews();
    configButtonView(DialogInterface.BUTTON_NEGATIVE, dialog, params, listener);
    configButtonView(DialogInterface.BUTTON_NEUTRAL, dialog, params, listener);
    configButtonView(DialogInterface.BUTTON_POSITIVE, dialog, params, listener);
    setVisibility(isGonePositiveButton() && isGoneNegativeButton() && isGoneNeutralButton() ? GONE : VISIBLE);
    return this;
  }

  private <D extends BaseDialog<?>> void configButtonView(int which, D dialog, FooterParams params, @Nullable OnClickButtonListener<D> listener) {
    final ButtonView buttonView;
    switch (which) {
      case DialogInterface.BUTTON_POSITIVE:
        if (params.positive.text == null) {
          return;
        }
        buttonView = new ButtonView(getContext(), which).configView(params.positive);
        mPositiveButton = buttonView;
        break;
      case DialogInterface.BUTTON_NEGATIVE:
        if (params.negative.text == null) {
          return;
        }
        buttonView = new ButtonView(getContext(), which).configView(params.negative);
        mNegativeButton = buttonView;
        break;
      case DialogInterface.BUTTON_NEUTRAL:
        if (params.neutral.text == null) {
          return;
        }
        buttonView = new ButtonView(getContext(), which).configView(params.neutral);
        mNeutralButton = buttonView;
        break;
      default:
        throw new IllegalArgumentException("Unknown button which.");
    }
    buttonView.setOnClickListener(v -> {
      if (listener == null) {
        dialog.dismiss();
      } else {
        listener.onClickButton(dialog, buttonView);
      }
    });
    addView(buttonView);
  }

  @Override
  public void removeAllViews() {
    mPositiveButton = null;
    mNegativeButton = null;
    mNeutralButton = null;
    if (getChildCount() > 0) {
      super.removeAllViews();
    }
  }

  @Nullable
  public ButtonView getPositiveButton() {
    return mPositiveButton;
  }

  @NonNull
  public ButtonView requirePositiveButton() {
    return Objects.requireNonNull(mPositiveButton);
  }

  @Nullable
  public ButtonView getNegativeButton() {
    return mNegativeButton;
  }

  @NonNull
  public ButtonView requireNegativeButton() {
    return Objects.requireNonNull(mNegativeButton);
  }

  @Nullable
  public ButtonView getNeutralButton() {
    return mNeutralButton;
  }

  @NonNull
  public ButtonView requireNeutralButton() {
    return Objects.requireNonNull(mNeutralButton);
  }

  public boolean isGonePositiveButton() {
    return mPositiveButton == null || mPositiveButton.getVisibility() == GONE;
  }

  public boolean isGoneNegativeButton() {
    return mNegativeButton == null || mNegativeButton.getVisibility() == GONE;
  }

  public boolean isGoneNeutralButton() {
    return mNeutralButton == null || mNeutralButton.getVisibility() == GONE;
  }
}